/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sheep.view.Component;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.RoundRectangle2D;
import java.awt.geom.RoundRectangle2D.Float;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.ListIterator;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import sheep.controller.Workspace;
import sheep.model.Note;
import sheep.view.AgendaPanel;
import sheep.view.TransitionManager;

/**
 *
 * @author geek
 */
public class DayGridPanelProject extends JPanel{


 private int columnLeftWidth, columnRightWidth;


 private GradientPaint   p1 = new GradientPaint(0, 0, new Color(0, 0, 0), 0, getHeight() - 1, new Color(100, 100, 100));
 private GradientPaint   p2 = new GradientPaint(0, 1, new Color(0, 0, 0, 50), 0, getHeight() - 3, new Color(255, 255, 255, 100)); 
 private GradientPaint   p4 = new GradientPaint(0, 1, new Color(200, 200, 0, 80), 0, getHeight() - 3, new Color(255, 255, 0, 100));
 
 private Float r2d4;
 private Shape clip1;
 private Graphics2D g2d;
 
 private final int pas = 20;
 
 private JPanel notePanel;
 
 //private Calendar currentDay;
 
 private JMenuItem ds = new JMenuItem("Nouvelle note");
 JPopupMenu popup = new JPopupMenu();
 JMenuItem es = new JMenuItem("Editer note");
 JMenuItem ss = new JMenuItem("supprimer note");
 
 
 private Workspace workSpace;
 
 public LinkedList<Note> noteList = new LinkedList<Note>();
  
 public LinkedList<Note> sortedList;
  
  GradientPaint gradient;

  public DayGridPanelProject( Workspace newWorkSpace){

    this.workSpace = newWorkSpace;
    noteList =  workSpace.getProjectNoteList();
    sortedList = new LinkedList<Note>();
     
    setOpaque(false);
    //this.setBackground(new java.awt.Color(150, 150, 255));

    popup.add(es); 
  
    es.addMouseListener(new MouseListener(){

      public void mouseClicked(MouseEvent e) {
       popup.setVisible(false);
      if (workSpace.getProjectSelectedNote().getTask()!= null){
        workSpace.setCurrentTask(workSpace.getProjectSelectedNote().getTask());
        TransitionManager.showProjectGraphEdit();
      }     
      }

      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) { }
      public void mouseExited(MouseEvent e) { }     
    });
     

    
    this.addMouseListener(new MouseListener(){

      public void mouseClicked(MouseEvent e) {
         popup.setVisible(false);     
         popup.remove(es);

         
         System.out.println(" day week project click" + workSpace.getCurrentDay().getTime().toString());         
         
                
                if (SwingUtilities.isRightMouseButton(e) && e.getX()>= columnLeftWidth){
                       // Pour les notes
                      if (sortedList != null){
                        ListIterator iter = sortedList.listIterator(sortedList.size());
                        while(iter.hasPrevious()){
                          Note testNote = (Note) iter.previous();
                          if (testNote.getDate().get(Calendar.DAY_OF_YEAR) == 
                                workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR))
                            if (testNote.getNoteZone()!=null)
                              if (e.getX()>= testNote.getNoteZone().getX() + testNote.getConcurrentPos()*15 && 
                                e.getX()<= testNote.getNoteZone().getX() + columnRightWidth&&
                                 e.getY()>= testNote.getNoteZone().getY() && e.getY()<= testNote.getNoteZone().getY()+ testNote.getNoteZone().height){
                                   // On definit comme selectionnÃ©                      
                                  //testNote.setSelected(true);                         
                                  workSpace.setProjectSelectedNote(testNote);
                                  popup.add(es);               
                                  popup.setLocation((int)(DayGridPanelProject.this.getLocationOnScreen().getX() + e.getX()), (int)(DayGridPanelProject.this.getLocationOnScreen().getY() +e.getY()));
                                  if (!popup.isVisible())
                                      popup.setVisible(true);

                              } 
                          }
                        }                                                                 
                      
                        
                  return;
                }
         
                if (e.getClickCount()==2 && e.getX()>= columnLeftWidth){
                       // Pour les notes
                      if (sortedList != null){
                        ListIterator iter = sortedList.listIterator(sortedList.size());
                        while(iter.hasPrevious()){
                          Note testNote = (Note) iter.previous();
                          if (testNote.getDate().get(Calendar.DAY_OF_YEAR) == 
                                workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR))
                            if (testNote.getNoteZone()!=null)
                              if (e.getX()>= testNote.getNoteZone().getX() + testNote.getConcurrentPos()*15 && 
                                e.getX()<= testNote.getNoteZone().getX() + columnRightWidth&&
                                 e.getY()>= testNote.getNoteZone().getY() && e.getY()<= testNote.getNoteZone().getY()+ testNote.getNoteZone().height){
                                   // On definit comme selectionnÃ©                      
                                  //testNote.setSelected(true);                         
                                  //workSpace.setSelectedNote(testNote);
                                if (testNote.getTask()!= null){
                                  workSpace.setCurrentTask(testNote.getTask());
                                  TransitionManager.showProjectGraphEdit();
                                }              
                                  //popup.add(ss);

                              } 
                          }
                        }                                                                 
                      
                  return;
                }
         
         
         
                workSpace.setProjectSelectedNote(null);
                
               repaint();
      }

      public void mousePressed(MouseEvent e) {}
      public void mouseReleased(MouseEvent e) {}
      public void mouseEntered(MouseEvent e) {}
      public void mouseExited(MouseEvent e) {}      
    });
  }

  
  void createGrid (Graphics g){
    
    Graphics2D g2d1 = (Graphics2D)g.create();
    
    
    g2d1.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
    g2d1.setColor(Color.WHITE);
    
    
    g2d1.fillRoundRect(columnLeftWidth, 1, columnRightWidth , 160-2, 8, 8);
    g2d1.fillRoundRect(1, 1, g.getClipBounds().width-2, 160-2, 8, 8);
    
    
    
    g.setColor(Color.DARK_GRAY);
    g.setFont(new java.awt.Font("Comic Sans MS", 1, 10));
    g.drawString( "Echéances" , 4, 15);
    g.drawString( "projet   :" , 4, 30);


    g.setFont(new java.awt.Font("Comic Sans MS", 1, 10));
    g.drawString( "Echéances" , 4,  70);
    g.drawString( "projet   :" , 4, 85);   
    
    g.setFont(new java.awt.Font("Comic Sans MS", 1, 10));
    g.drawString( "Echéances" , 4,  130);
    g.drawString( "projet   :" , 4, 145);      
    

    
    
    //for (int i = columnWidth-10, j = 0; i < g.getClipBounds().width; i+=columnWidth, j++){
      //g.setColor(Color.DARK_GRAY);
      //g.drawRoundRect(columnWidth, i-15, 7*columnWidth, 20, 8, 8);
      g.setColor(Color.LIGHT_GRAY);
      g.drawLine(columnLeftWidth, 1, columnLeftWidth, 159);
      g.drawLine(columnLeftWidth+ columnRightWidth-1, 1, columnLeftWidth+ columnRightWidth-1, 159);

   // }
    
  }  
  

  void paintNote(Note testNote, Graphics2D g2d ){
     
    Calendar cal = Calendar.getInstance();
    cal.setTime(workSpace.getCurrentDay().getTime());
    cal.add(Calendar.DAY_OF_YEAR, -cal.get(Calendar.DAY_OF_MONTH));
    cal.add(Calendar.DAY_OF_YEAR, -(cal.get(Calendar.DAY_OF_WEEK)-2));
    
    // Initialisation de la position de dessin
    int PosX = columnLeftWidth;
    int longueur= pas;
    //Zone de dessin
    // Si la note n'a jamais Ã©tÃ© dessinÃ© ou si l'on a redimmensionnÃ©
    //if (testNote.getNoteZone()==null || testNote.getNoteZone().width!=columnWidth ){
      
      int PosY = testNote.getConcurrentPos()*20;
      // On est prÃ©cis Ã  la demi heure

      if (testNote.isSelected())
       longueur = 2*pas;
      else
        longueur = pas;
      
      testNote.setNoteZone(new RoundRectangle2D.Float( PosX,PosY, columnRightWidth , longueur- 1, 20, 20));    
              
      testNote.setXPos(PosX);
      testNote.setYPos( PosY);
    //}
      

        longueur = pas;
      // On impose la position de dessin
      testNote.setNoteZone(new RoundRectangle2D.Float( testNote.getNoteZone().x , 
              testNote.getNoteZone().y, columnRightWidth , longueur- 1, 20, 20));      
      
    gradient = new GradientPaint((int)testNote.getNoteZone().getX(),(int)testNote.getNoteZone().getY(),testNote.getColor(),
                                (int)testNote.getNoteZone().getX()+ (int)(testNote.getNoteZone().getWidth()*1.4) ,
                                (int)testNote.getNoteZone().getY(),Color.WHITE);
   
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
    
    clip1 = g2d.getClip();
    
    //int effectiveWidth = columnWidth - (testNote.getConcurrentPos()*15);
    //testNote.setNoteZone(new RoundRectangle2D.Float( PosX,testNote.getNoteZone().y+testNote.getConcurrentPos()*20, columnWidth , longueur- 1, 20, 20));    
              
    //testNote.setYPos((int) testNote.getNoteZone().y+testNote.getConcurrentPos()*20);
      
    g2d.clip(testNote.getNoteZone());
    //g2d.setPaint(p3);
    g2d.setColor(testNote.getColor());
    g2d.setClip(clip1);
    g2d.fillRoundRect( columnLeftWidth ,
                      (int)(testNote.getNoteZone().y), columnRightWidth, (int)testNote.getNoteZone().height,18, 18);
    

    // Contour foncÃ©   
    g2d.setPaint(p1);    
    g2d.drawRoundRect( columnLeftWidth   , 
                      (int)(testNote.getNoteZone().y), columnRightWidth, (int)testNote.getNoteZone().height- 1, 20, 20);        
   

      g2d.setPaint(p2);
    
    g2d.drawRoundRect(1+ columnLeftWidth    ,
                      (int)(testNote.getNoteZone().y)+1, columnRightWidth, (int)testNote.getNoteZone().height - 3, 18, 18);
    
    
    // Bas de la note
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.9f));
    Rectangle rectClipBottom = new Rectangle (columnLeftWidth   , 
                              (int)(testNote.getNoteZone().y+testNote.getNoteZone().getHeight())-20, columnRightWidth, 20);
    g2d.clip(rectClipBottom);
    g2d.setPaint(gradient);
    g2d.fillRoundRect( columnLeftWidth +1  , 
                      (int)(testNote.getNoteZone().y), columnRightWidth-2, (int)testNote.getNoteZone().getHeight()-1,18, 18);

    

      g2d.setColor(Color.WHITE);
    
    g2d.setFont(new java.awt.Font("Comic Sans MS", 1, 9));
    g2d.drawString( testNote.getDate().getTime().toString().substring(11, 16) +"->"+
                    testNote.getEndDate().getTime().toString().substring(11, 16), 
                    columnLeftWidth    +5,
                    (int)(testNote.getNoteZone().y+testNote.getNoteZone().getHeight())-7);
    g2d.setClip(clip1);   
    
    // Titre de la note    
    g2d.setPaint(gradient);
    Rectangle rectClip = new Rectangle (columnLeftWidth  ,
                      (int)(testNote.getNoteZone().y), columnRightWidth, (int)testNote.getNoteZone().height);
    g2d.clip(rectClip);    
    g2d.fillRoundRect( columnLeftWidth  , 
                      (int)(testNote.getNoteZone().y), columnRightWidth, (int)testNote.getNoteZone().height,18, 18);

    g2d.setColor(Color.WHITE);
    

    g2d.drawString(testNote.getTitle(), columnLeftWidth   +5,
                    (int)(testNote.getNoteZone().y)+13);

    g2d.setClip(clip1);
   
  }
  
  
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    noteList = new LinkedList<Note>();
    sortedList = new LinkedList<Note>();    
    workSpace.generateProjectList();
    noteList =  workSpace.getProjectNoteList();
    columnLeftWidth = 90;
    columnRightWidth = getWidth() -columnLeftWidth; 
    // Dessin de la grille
    g.setColor(new Color(100,100,100));
    createGrid(g);
    // Positionnement en X des fleches

    // Recuperation du graphics de dessin
    g2d = (Graphics2D)g.create();
   
    clip1 = g2d.getClip();
    
    
    
        //Aujourd'hui ??
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
    
  
    Calendar temp1 = Calendar.getInstance();
    temp1.setTime(workSpace.getCurrentDay().getTime());
    
    int k = temp1.get(Calendar.DAY_OF_YEAR)- Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
       if (k ==0 ){
         
                    
          //System.out.println(" AUJOURD'HUI");
          r2d4 = new RoundRectangle2D.Float(columnLeftWidth , 1,
                  columnRightWidth, 158, 20, 20);
             // Zone du matin
          g2d.clip(r2d4);
          g2d.setColor(new Color(210,210, 255));
          g2d.fillRoundRect(columnLeftWidth , 1,
                  columnRightWidth-2, 158, 20, 20);
          g2d.setClip(clip1);
          //g2d.setPaint(p1);
          g2d.setColor(new Color(130,130, 255));
          g2d.drawRoundRect(columnLeftWidth , 1 ,
                  columnRightWidth-1, 158, 20, 20);
          //g2d.setPaint(p2);
          g2d.setColor(new Color(158,200, 255));
          g2d.drawRoundRect(columnLeftWidth , 1 ,
                  columnRightWidth-2, 298-1, 18, 18);
    }
       

    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.6f));
    
    updateConcurrent();
    
    
    for ( Note testNote : sortedList ){
      if (testNote.getDate().get(Calendar.DAY_OF_YEAR) == 
              workSpace.getCurrentDay().get(Calendar.DAY_OF_YEAR)){
          paintNote(testNote,g2d);
      }
    }

    g2d.dispose();
  }
  
  
  int getConcurentCout(Note note){
    ListIterator iter = sortedList.listIterator(sortedList.indexOf(note));
      while(iter.hasPrevious()){
        Note temp = (Note) iter.previous();
          if (note.getDate().get(Calendar.DAY_OF_YEAR)== temp.getDate().get(Calendar.DAY_OF_YEAR)){
              if (temp.isSelected())
                return temp.getConcurrentPos()+2;     
          return temp.getConcurrentPos()+1;              
          }
      }    
    return 0;
  }
  
  void updateConcurrent(){
    
    if (noteList.size()>0){
      sortedList = new LinkedList<Note>();
      sortedList.add(noteList.element());
      noteList.element().setConcurrent(0);
      noteList.element().setConcurrentPos(0);

      for ( Note testNote1 : noteList.subList(1, noteList.size()) ){
          // Initialisation des valeurs
          testNote1.setConcurrent(0);
          testNote1.setConcurrentPos(0);   
          // CrÃ©ation de la liste triÃ©e
          ListIterator iter = sortedList.listIterator();
          // Tant qu'il y a un element dans la list ou que celui ci est infÃ©rieur Ã  la valeur
          // et si les element graphiques sont crÃ©Ã©es
          while(iter.hasNext() && 
                  testNote1.getDate().get(Calendar.DAY_OF_YEAR)>= sortedList.get(iter.nextIndex()).getDate().get(Calendar.DAY_OF_YEAR)
                   ){
            // On avance
            iter.next();
          }
          iter.add(testNote1);      
      }

      // On trouve le nombre de pere de chaque note
      // (nombre maximum de note concurrente
      for ( Note testNote1 : sortedList ){        
        testNote1.setConcurrentPos(getConcurentCout(testNote1));
      }
    }
  }
  
  
}
